package com.zys.el.time.controller;

import com.zys.el.time.common.ResultCode;
import com.zys.el.time.service.impl.AbsorbedDealService;
import com.zys.el.time.service.impl.AbsorbedTargetService;
import com.zys.el.time.utils.TimeUtil;
import com.zys.el.time.vojo.AbsorbedAnalysis;
import com.zys.el.time.vojo.AbsorbedDetail;
import com.zys.el.time.vojo.Result;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/absorbed")
@Api(tags = "专注分析接口")
public class AbsorbedController {
    @Resource
    private AbsorbedDealService abDealService;
    @Resource
    private AbsorbedTargetService abTargetService;

    @GetMapping("/analysis")
    @ApiOperation("获取时间管理分析数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "firstDate", value = "时间范围上区间", required = true),
            @ApiImplicitParam(name = "lastDate", value = "时间范围下区间", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", required = true)
    })
    public Result getAnalysis(@RequestParam("firstDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate firstDate,
                              @RequestParam("lastDate") @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate lastDate,
                              @RequestParam("userId") Long userId) {
        log.debug("{}, {}, {}",firstDate, lastDate, userId);
        lastDate = lastDate.plusDays(1);
        AbsorbedAnalysis dealAnalysis = abDealService.getDealAnalysis(firstDate, lastDate, userId);
        AbsorbedAnalysis targetAnalysis = abTargetService.getTargetAnalysis(firstDate, lastDate, userId);
//        log.debug(dealAnalysis.toString());
        // 用作排序
        ArrayList<AbsorbedDetail> allAbsorbedList = new ArrayList<>();

        AbsorbedAnalysis res = new AbsorbedAnalysis();
        res.setUserId(userId);

        List<AbsorbedDetail> dealDetailList = dealAnalysis.getAbsorbedDetailList();
        if (dealDetailList != null && dealDetailList.size() != 0) {
            allAbsorbedList.addAll(dealDetailList);
        }

        List<AbsorbedDetail> targetDetailList = targetAnalysis.getAbsorbedDetailList();
        if (targetDetailList != null && targetDetailList.size() != 0) {
            allAbsorbedList.addAll(targetDetailList);
        }

        // 设置总专注次数
        res.setAbsorbedTimes((dealAnalysis.getAbsorbedTimes() != null ? dealAnalysis.getAbsorbedTimes() : 0)
                + (targetAnalysis.getAbsorbedTimes() != null ? targetAnalysis.getAbsorbedTimes() : 0)
        );

        // 设置总专注时长
        res.setAbsorbedDuration(TimeUtil.handleTimeBackHourMinSec(dealAnalysis.getAbsorbedDuration() != null ? dealAnalysis.getAbsorbedDuration() : "0:0:0",
                targetAnalysis.getAbsorbedDuration() != null ? targetAnalysis.getAbsorbedDuration() : "0:0:0")
        );

        // 排倒序
        allAbsorbedList.sort((o1, o2) -> TimeUtil.timeCompare(o2.getAllMinutes(), o1.getAllMinutes()));

//        log.debug(allAbsorbedList.toString());
        res.setAbsorbedDetailList(allAbsorbedList);
        log.debug(res.toString());
        return Result.ok(res);
    }


}
